<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace app\middleware;

use App\user\cache\UserTokenCache;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

/**
 * Class StaticFile
 * @package app\middleware
 */
class CheckUserLogin implements MiddlewareInterface
{
    public function process(Request $request, callable $next): Response
    {
        $token = $request->header('token', '');
        if (empty($token)) return response(error('操作失败，请重新登陆后再试！'), 400);

        $info = UserTokenCache::get($token);
        if (empty($info)) return response(error('账号登录状态时效已过，请重新登陆！'), 400);
        $request->token = $token;
        return $next($request);
    }
}
